
import java.util.List;

public class 编写equals方法 {
    public static void main(String[] args) {
        //test1();
        test2();
    }
    static void test2(){
        List<Person> list = List.of(
            new Person("XiaoMing", 18),
            new Person("XiaoHong", 25),
            new Person("BobSmith", 20)
        );
        boolean exist = list.contains(new Person("BobSmith", 20));
        System.out.println(exist ? "测试成功!" : "测试失败!");
    }
    static void test1(){
        List<Person> list=List.of(
            new Person("huahsou", 12),
            new Person("lihua", 23),
            new Person("xiaozhang", 22));
        System.out.println(list.contains(new Person("lihua", 23)));
    }
}
class Person{
    String name;
    int age;
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    @Override
    public boolean equals(Object o){
        if(o instanceof Person){
            Person p=(Person) o;
            boolean nameEquals=false;
            if(this.name==null&&p.name==null){
                nameEquals=true;
            }
            if(this.name!=null){
                nameEquals=this.name.equals(p.name);
            }
            return nameEquals&&this.age==p.age;
        }
        else{
            return false;
        }
    }
}